package com.wjc.myktapplication.cusview

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.wjc.common.dp
import com.wjc.basemodule.utils.LogTools

/**
 * ClassName: com.wjc.myktapplication.cusview
 * Description:圆形
 * JcChen on 2021.08.31.23:29
 */
class CircleView(
    context: Context,
    attrs: AttributeSet) : View(context, attrs) {
    private val TAG = "CircleView"
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)

    private var RADIUS = 100f.dp
    private var PADDING = 100f.dp

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val size = ((RADIUS + PADDING) * 2).toInt()
//        val specWidth = MeasureSpec.getSize(widthMeasureSpec)
//        val specHeight = MeasureSpec.getSize(heightMeasureSpec)
//        val width = when (MeasureSpec.getMode(widthMeasureSpec)) {
//            MeasureSpec.EXACTLY -> specWidth
//            MeasureSpec.AT_MOST -> size.coerceAtMost(specWidth)
//            else -> size
//        }
//
//        val height = when (MeasureSpec.getMode(heightMeasureSpec)) {
//            MeasureSpec.EXACTLY -> specHeight
//            MeasureSpec.AT_MOST -> size.coerceAtMost(specHeight)
//            else -> size
//        }
        val width = resolveSize(size, widthMeasureSpec)
        val height = resolveSize(size, heightMeasureSpec)
        LogTools.d(TAG, ": width=$width,height=$height")
        val resultSize = width.coerceAtMost(height)
        setMeasuredDimension(resultSize, resultSize)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawCircle(RADIUS + PADDING, RADIUS + PADDING, RADIUS, paint);
    }
}